package com.solming.leetcode.question;
//给定一个字符串，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。
//输入："Let's take LeetCode contest"
//输出："s'teL ekat edoCteeL tsetnoc"
public class ReverseWords557 {

    public static String reverseWords(String s) {
        char[] resultArr = s.toCharArray();
        int len = s.length();
        int i = 0;
        while (i < len) {
            int index = s.indexOf(" ", i);
            int end = len-1;
            int start = i;
            if (index != -1) {
                end = index - 1;
                i = index + 1;
            } else {
                i = end+1;
            }

            while (start < end) {
                char temp = resultArr[start];
                resultArr[start] = resultArr[end];
                resultArr[end] = temp;
                start++;
                end--;
            }


        }
        return new String(resultArr);
    }


    public static void main(String[] args) {
        System.out.println(reverseWords("Let's take LeetCode contest"));
        System.out.println("s'teL ekat edoCteeL tsetnoc");
        System.out.println("s'teL ekat edoCteeL tsetnoc".equals(reverseWords("Let's take LeetCode contest")));
    }


}
